package com.lin.filegraph.utils.moduledg;

import com.lin.filegraph.utils.filedg.FileGraph;
import com.lin.filegraph.utils.filedg.FileGraphNode;
import com.lin.filegraph.utils.parser.node.JProjectNode;

import java.util.ArrayList;
import java.util.List;

import static com.lin.filegraph.utils.extractor.JavaInfoExtractor.getFileGraph;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 15:42
 */

public class ModuleNode implements Comparable<ModuleNode> {
    private int ID;//唯一id
    private String ModulePath;//模块路径
    private String ModuleName;//节点名
    private List<FileGraphNode> listOfFiles;//包含的文件
    private int locOfCom = 0;//有效代码行
    private FileGraph fileGraph;

    private int callDepth;//调用深度

    public int getCallDepth() {
        return callDepth;
    }

    public void setCallDepth(int callDepth) {
        this.callDepth = callDepth;
    }

    public ModuleNode() {


    }

    public ModuleNode(String ModuleName) {
        this.ModuleName = ModuleName;
    }


    public ModuleNode(String ModuleName, int id, int loc, FileGraph fileGraph, String ModulePath) {
        this.ModuleName = ModuleName;
        this.ID = id;
        this.locOfCom = loc;
        this.fileGraph = fileGraph;
        this.ModulePath = ModulePath;
        //Tom 加的
        this.listOfFiles = this.fileGraph.getListOfFiles();
    }

    public ModuleNode(String ModuleName, String ModulePath, int id, JProjectNode root, int loc) {
        this.ID = id;
        this.ModuleName = ModuleName;
        this.ModulePath = ModulePath;
        if (!ModuleName.contains(".java")) {
            FileGraph fileGraph = getFileGraph(ModulePath);
            fileGraph.filterloc(loc);
            this.fileGraph = fileGraph;
            this.listOfFiles = fileGraph.getListOfFiles();
        } else {
            this.ModuleName = ModuleName.substring(0, ModuleName.lastIndexOf("."));
            List<String> file = new ArrayList<>();
            int index = root.getProjectPath().length() + 1;
            String filePath = ModulePath.substring(index);
            System.out.println(filePath);
            file.add(filePath);
            this.fileGraph = root.getFdg().getSubFDGByFileStrs(file);
            this.listOfFiles = this.fileGraph.getListOfFiles();
        }

    }

    public String getModuleName() {
        return ModuleName;
    }

    public void setModuleName(String str) {
        this.ModuleName = str;
    }

    public List<FileGraphNode> getAllFiles() {
        return listOfFiles;
    }

    public void addFile(FileGraphNode file) {
        if (!listOfFiles.contains(file))
            listOfFiles.add(file);
    }

    public int getLocOfCom() {
        return locOfCom;
    }

    public void calLocOfModule(List<FileGraphNode> files) {
        for (FileGraphNode file : files) {
            this.locOfCom += file.getValidLOC();
        }
    }

    public int getLocOfModule() {
        return this.locOfCom;
    }

    @Override
    public String toString() {
        return "[ModuleNode]  " + ModuleName + " " + listOfFiles.size() + " " + listOfFiles;
    }

    public int getID() {
        return ID;
    }

    // 返回文件依赖图
    public FileGraph getfileGraph() {
        return this.fileGraph;
    }

    public void setFileGraph(FileGraph fdg) {
        this.fileGraph = fdg;
    }

    public void setID(int num) {
        this.ID = num;
    }

    public String getModulePath() {
        return ModulePath;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(ModuleNode o) {
        return this.getModuleName().compareTo(o.getModuleName());
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ModuleNode)) return false;
        ModuleNode node = (ModuleNode) o;
        if (this == o) return true;
        return node.ID == this.ID && node.ModuleName.equals(this.ModuleName) && node.ModulePath.equals(this.ModulePath);
    }

    @Override
    public int hashCode() {
        int res = Integer.valueOf(ID).hashCode();
        res = 17 * res + ModuleName.hashCode();
        res = 17 * res + ModulePath.hashCode();
        return res;
    }
}

